/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aktor;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ASUS 1215P
 */
public class artikel {
    public int Id_Artikel;
    public String FileUpload;
    public String Judul_Artikel;
    public String Cover;
    public String koneksi = "jdbc:Oracle:thin:@localhost:1521:XE";
    public String username = "hr";
    public String password = "110590";
    public Connection con = null;
    public Statement stmt = null;
    public ResultSet rs = null;
    public DatabaseMetaData dmd;
    public ResultSetMetaData rsmd;
    
     
     public void connect() throws SQLException {
        con = DataBaseConnection.getDBConnection();
    }


    /**
     * @return the Id_Artikel
     */
    public int getId_Artikel() {
        return Id_Artikel;
    }

    /**
     * @param Id_Artikel the Id_Artikel to set
     */
    public void setId_Artikel(int Id_Artikel) {
        this.Id_Artikel = Id_Artikel;
    }

    /**
     * @return the FileUpload
     */
    public String getFileUpload() {
        return FileUpload;
    }

    /**
     * @param FileUpload the FileUpload to set
     */
    public void setFileUpload(String FileUpload) {
        this.FileUpload = FileUpload;
    }

    /**
     * @return the Judul_Artikel
     */
    public String getJudul_Artikel() {
        return Judul_Artikel;
    }

    /**
     * @param Judul_Artikel the Judul_Artikel to set
     */
    public void setJudul_Artikel(String Judul_Artikel) {
        this.Judul_Artikel = Judul_Artikel;
    }

    /**
     * @return the Cover
     */
    public String getCover() {
        return Cover;
    }

    /**
     * @param Cover the Cover to set
     */
    public void setCover(String Cover) {
        this.Cover = Cover;
    }
    
    
//    public void insertArtikel() throws SQLException {
//        Connection con = DataBaseConnection.getDBConnection();
//
//        String SQL = "insert into artikel (judul_artikel, artikel_file, artikel) values (?,?,?)";
//
//        PreparedStatement ps = con.prepareStatement(SQL);
//
//        ps.setString(1, this.Id_Artikel);
//        ps.setString(2, this.Judul_Artikel);
//        ps.setString(3, this.Cover);
//        ps.setString(4, this.FileUpload);
//        
//        int x = ps.executeUpdate();
//        
//        ps.close();
//        con.close();
//    }
    
    //method autogenret untuk id artikel
    public int ID_Artikel() throws SQLException  {
        int id = 0;
        Statement st = null;
        ResultSet res = null;
        try {
            String query = "SELECT MAX(ID_ARTIKEL) FROM ARTIKEL";
            st = DataBaseConnection.getDBConnection().createStatement();
            res = st.executeQuery(query);

            while (res.next()) {
                id = res.getInt(1) + 1;
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
            System.out.println("gagal");
        }
        return id;
    }
    
     /*
     * method dibawah digunakan untuk menampilkan data yang ada di data base dalam bentuk tabel.
     */
    public String aksesDatabase() {
        String dbHTML = "";

        if (koneksi == null) {
            password = "";
            return dbHTML;
        }
        if (koneksi.equals("")) {
            password = "";
            dbHTML = "Koneksi error. Mungkin nama tabel kosong.";
            return dbHTML;
        }
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
//            con = DriverManager.getConnection(koneksi,
//                    "system", "hr");
            con = DriverManager.getConnection("jdbc:Oracle:thin:@172.23.9.185:1521:orcl",
                    "MHS105314091", "MHS105314091");
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM ARTIKEL ORDER BY ID_ARTIKEL");
            dmd = con.getMetaData();
            rsmd = rs.getMetaData();
            int nColumn = rsmd.getColumnCount();
            dbHTML += "<table border=\"1\" cellpadding=\"4\"><tr>";
            for (int i = 1; i <= nColumn; i++) {
                dbHTML += "<th>" + rsmd.getColumnName(i) + "</th>";
            }
            dbHTML += "</tr>";
            while (rs.next()) {
                dbHTML += "<tr>";
                for (int i = 1; i <= nColumn; i++) {
                    dbHTML += "<td>" + rs.getString(i) + "</td>";
                }
                dbHTML += "</tr>";
            }
            dbHTML += "</table>";
        } catch (ClassNotFoundException e) {
            dbHTML += "<p>Error loading driver: " + e.getMessage() + "</p>";
        } catch (SQLException e) {
            dbHTML += "<p>Error SQL: " + e.getMessage() + "</p>";
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException sqle) {
            }
        }
        password = "";
        return dbHTML;
    }
    
    /*
     * method dibawah digunakan untuk menampilkan daftar buku yang ada pada database.
     */
    public ArrayList<artikel> getDisplay() {
        String sql;
        ArrayList vCatalog = new ArrayList<artikel>();
        try {
            sql = "SELECT * from Artikel ORDER BY  Id_Artikel";
            connect();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                artikel a = new artikel();
                a.setJudul_Artikel(rs.getString("Judul_Artikel"));
                a.setCover(rs.getString("cover"));
                a.setFileUpload(rs.getString("fileupolad"));
                vCatalog.add(a);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return vCatalog;
    }
    
    public static void main(String[] args) {
        ArrayList<artikel> A= new aktor.artikel().getDisplay();
        for (int i = 0; i < A.size(); i++) {
            System.out.println(A.get(i).getJudul_Artikel());
            
        }
    }
}
